package bairui.international.console.web.controller.order.product;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;

import bairui.international.model.domain.jpa.QTB_ProductOrder;
import bairui.international.model.domain.jpa.TB_ProductOrder;
import bairui.international.service.Service_ProductOrder;

/**
 *
 * 列表
 *
 * @generated
 * 
 * @see STest_C_ProductOrder_Page_Page 测试
 *
 */
@Controller
public class C_ProductOrder_Page_Page
 extends SC_ProductOrder_Page_Page 
{
	
	@Autowired private Service_ProductOrder service_ProductOrder;
	
	/**
	 * 分页列表
	 */
	@SuppressWarnings("serial")
	@Action(Page.class)
	@GetMapping(value=Page.RequestMappingValue, consumes=MediaType.APPLICATION_JSON_VALUE)
	public @FomatOutput @ResponseBody Object page(
		HttpServletRequest request
	) throws Exception {
		org.springframework.data.domain.Page<TB_ProductOrder> page = service_ProductOrder.page(
				it->filter(it, request), 
				Page.Output.page(request), 
				it->sort(it, request)
			);
		//
		return Page.Output.output(
				page
					.map(it->new Page.Output.Model(it){
							@Override protected void assign_before() {
							}
							@Override public String assignUserName() {
								if(null == it.getUserInfo()){
									return "";
								}
								return it.getUserInfo().getRealName();
							}
						}
					)
			);
	}
	
	static Sort sort(QTB_ProductOrder it, HttpServletRequest request) {
		return Page.Output.sort(it, request, new Page.Output.OrderExtends() {
			@Override public Sort gainUserName(Direction direction) {
				return sort(direction, it.userInfo.realName);
			}
			@Override public Sort gainBranchCompany(Direction direction) {
				return sort(direction, it.branchCompany.name);
			}
			@Override public Sort gainArea(Direction direction) {
				return sort(direction, it.area.name);
			}
		});
	}

	static BooleanBuilder filter(QTB_ProductOrder it, HttpServletRequest request){
		return Page.Filter.query(it, request,new Page.Filter.FilterExtends() {
			@Override public Predicate gainUserName(String userName) {
				return it.userInfo.realName.like("%" + userName + "%");
			}
		});
	}

}
